import { BasicColumn, FormSchema } from '/@/components/Table';
import { defaultRule, rules } from '@/utils/common/validator';
import { apiGetAccountCheckNumberIsExistQuery } from '@/api/http/system/basic/account/account.api';
import { apiGetDeptsDropdown } from '@/api/http/system/senior/depts/depts.api';
import {
  userDatePickerFormItem,
  userRemarkFormItem,
  userStatusFormItem,
} from '@/settings/ShareFormItem';
import { userRemarksColumn, userStatusColumn } from '@/settings/ShareTableColumns';

export const useAccountUpdateSchemaForm = (): FormSchema[] => {
  return [
    { field: 'name', label: '账户名称', component: 'Input', rules: defaultRule() },
    {
      field: 'number',
      label: '账户编号',
      component: 'Input',
      dynamicRules: ({ model, schema }) =>
        rules.uniqueFieldValidation(
          apiGetAccountCheckNumberIsExistQuery,
          'id',
          schema,
          model,
          true,
        ),
    },
    {
      field: 'deptId',
      label: '所属组织',
      component: 'ApiTreeSelect',
      required: true,
      componentProps: { api: apiGetDeptsDropdown },
    },
    userDatePickerFormItem({ label: '余额日期', field: 'date', required: true }),
    {
      field: 'initial',
      label: '期初余额',
      component: 'Input',
      defaultValue: '0',
      componentProps: {
        suffix: '￥',
      },
      rules: [{ required: true }, rules.validateNotEmpty('numerical2', '期初余额不正确')],
    },

    userStatusFormItem(),
    userRemarkFormItem(),
    { field: 'id', label: '用户id', component: 'Input', show: false },
  ];
};

export const useAccountListColumns = (): BasicColumn[] => {
  return [
    // { title: 'ID', dataIndex: 'id' },
    { title: '账户名称', dataIndex: 'name' },
    { title: '账户编号', dataIndex: 'number' },
    { title: '所属组织', dataIndex: 'deptName' },
    // { title: '余额日期', dataIndex: 'date' },
    // { title: '期初余额', dataIndex: 'initial' },
    { title: '账户余额', dataIndex: 'balance' },

    userStatusColumn(),
    userRemarksColumn(),
  ];
};

export const useAccountListSearchForm = (): FormSchema[] => {
  return [
    { field: 'name', label: '账户名称', component: 'Input' },
    { field: 'number', label: '账户编号', component: 'Input' },
  ];
};
